from ...bases import simple

from widget_classes import Widgets


class HBox(Widgets.BoxLayout):
    pass
    
class VBox(HBox):
    def __init__(self, **kwargs):
        kwargs.setdefault('orientation', 'vertical')
        super(VBox, self).__init__(**kwargs)
    
class Table(Widgets.GridLayout):
    pass

class ScrolledWindow(Widgets.ScrollView):
    pass
    
class Frame(VBox):
    def __init__(self, **kwargs):
        super(Frame, self).__init__(**kwargs)
        self.label = Label(**kwargs)
    
class Label(Widgets.Label):
    def __init__(self, **kwargs):
        lbl = kwargs.get('label')
        if lbl:
            kwargs.setdefault('text', lbl)
        super(Label, self).__init__(**kwargs)

class Button(Widgets.Button):
    def __init__(self, **kwargs):
        label = kwargs.get('label')
        if label:
            kwargs.setdefault('text', label)
        super(Button, self).__init__(**kwargs)
        
class ToggleBtn(simple.Toggle):
    def setup_widgets(self, **kwargs):
        lbl = kwargs.get('label')
        if lbl:
            kwargs.setdefault('text', lbl)
        self.widget = Widgets.ToggleButton(**kwargs)
        self.widget.bind(state=self.on_widget_toggled)
    def get_widget_state(self):
        return self.widget.state
    def set_widget_state(self, value):
        self.widget.state = value
        
class Slider(simple.Fader):
    def setup_widgets(self, **kwargs):
        kwargs.setdefault('max', 255)
        d = dict(value_min='min', value_max='max', lower='min', upper='max')
        w_kwargs = {}
        for key, val in kwargs.get('adj_kwargs', {}).iteritems():
            if key in d:
                w_kwargs.update({d[key]:val})
        for key, val in kwargs.iteritems():
            if key in d:
                w_kwargs.update({d[key]:val})
        w_kwargs.setdefault('orientation', self.orientation)
        self.widget = Widgets.Slider(**w_kwargs)
        self.widget.bind(value=self.on_widget_change_value, 
                         on_touch_down=self.on_widget_button_press, 
                         on_touch_up=self.on_widget_button_release)
    def get_widget_value(self):
        return self.widget.value
    def set_widget_value(self, value):
        if value is None:
            value = 0
        self.widget.value = value
    
class HSlider(Slider):
    orientation = 'horizontal'
    
class VSlider(Slider):
    orientation = 'vertical'
    
    
class Entry(simple.EntryBuffer):
    def setup_widgets(self, **kwargs):
        self.name = kwargs.get('name', '')
        self.topwidget = Frame(**kwargs)
        self.widget = Widgets.TextInput(multiline=False)
        self.widget.bind(focus=self.on_widget_focus)
        self.topwidget.add_widget(self.widget)
    def on_widget_focus(self, widget, value):
        if not value:
            self.on_widget_value_changed()
    def get_widget_text(self):
        return str(self.widget.text)
    def set_widget_text(self, text):
        self.widget.text = text
